
module.exports = {
	deletePayroll:'DELETE FROM product_payroll WHERE ID=?',
	getPayrollAll:function(param){
		var paramArr=[] , sqlParm=[] , sqlStr = 'SELECT * FROM product_payroll ORDER BY MIN_INTEREST ASC';
        var pLen = 0;
        for(var p in param){
            pLen++;
        }
        if(pLen > 0){
            for(var i in param){
                if(param[i] && param[i].length>0){
                    var str = '';
                    if(i == 'BANK_NAME' || i=='REMARK' || i=='REPAYMENT_WAY_REMARK'){
                        str=i+' like "%'+param[i]+'%"';
                    }else if(i=='TERM' || i=='CREDIT_ASK_ID' || i=='DATUM_ID' || i=='DEBT_ASK_ID' || i=='REPAYMENT_WAY'){
                        var prs = param[i].split(',');
                        var prsArr=[];
                        for(var s in prs){
                            prsArr.push('find_in_set("'+prs[s]+'",'+i+')');
                        }
                        str = prsArr.join(' OR ');
                    }else if(i.indexOf('MIN_')!=-1){
                        str=i+'>='+param[i];
                    }else if(i.indexOf('MAX_')!=-1){
                       str=i+'<='+param[i];
                    }else{
                        str=i+'="'+param[i]+'"';
                    }
                    paramArr.push(str);
                    sqlParm.push(param[i]);
                }
            }
            if(paramArr.length > 0){
                sqlStr = 'SELECT * FROM product_payroll WHERE '+paramArr.join(' AND ')+' ORDER BY MIN_INTEREST DESC';
            }
        }
        return sqlStr;
	},
	insertPayroll:function(param){
		var nameArr=[],valueArr=[];
		for(var i in param){
			if(i != 'ID' && param[i]){
				nameArr.push(i);
				valueArr.push("'"+param[i]+"'");
			}
		}
		return 'INSERT INTO product_payroll('+nameArr.join(',')+') VALUES('+valueArr.join(',')+')';
	},
	uploadPayroll:function(param){
		var setNameArr=[];
		for(var i in param){
			if(i != 'ID'){
				setNameArr.push(i+'='+"'"+param[i]+"'");
			}
		}
		return 'UPDATE product_payroll SET ' + setNameArr.join(',') + ' WHERE ID='+param.ID;
	}
};